//
//  MyTabBar.m
//

#import "MyTabBar.h"

@interface MyTabBar()<MyTabBarButtonDelegate>

@property (nonatomic, weak) MyTabBarButton *selectedButton;

@property(nonatomic, strong) NSMutableArray *tabbarBtnArray;

@property (nonatomic, strong) NSMutableArray *norImageArrM;

@property (nonatomic, strong) NSMutableArray *selImageArrM;

@property (nonatomic, strong) UIImageView *bgImageView;

@end

@implementation MyTabBar

- (NSMutableArray *)tabbarBtnArray {
    if (!_tabbarBtnArray) {
        _tabbarBtnArray = [NSMutableArray array];
    }
    return  _tabbarBtnArray;
}

- (NSMutableArray *)norImageArrM {
    if (!_norImageArrM) {
        _norImageArrM = [NSMutableArray array];
    }
    return _norImageArrM;
}

- (NSMutableArray *)selImageArrM {
    if (!_selImageArrM) {
        _selImageArrM = [NSMutableArray array];
    }
    return _selImageArrM;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_TabbarBg"]];
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
       if (self) {
//           self.backgroundImage = [UIImage imageNamed:@"icon_login_bg"];
           self.barTintColor =  RGB(4, 44, 8);
            
           [self addSubview:self.bgImageView];
           self.bgImageView.frame = CGRectMake(15, 0, kScreenWidth - 30, DimensionBaseIphone6(54));
           
//           [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//               make.left.equalTo(self).offset(15);
//               make.right.equalTo(self).offset(-15);
//               make.height.equalTo(@54);
//               make.top.equalTo(self);
//           }];
        
       }
    return self;

}


- (void)addTabBarButtonNorImageUrl:(NSString *)norImageUrl
                       selImageUrl:(NSString *)selImageUrl
                             title:(NSString *)title withSel:(NSInteger)index{

    MyTabBarButton *tabBarBtn = [[MyTabBarButton alloc] init];
    tabBarBtn.delegate = self;
    [self addSubview:tabBarBtn];

    [tabBarBtn setTabBarImageUrl:norImageUrl title:title];

    [tabBarBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];

    [self.tabbarBtnArray addObject:tabBarBtn];
    [self.norImageArrM addObject:norImageUrl];
    [self.selImageArrM addObject:selImageUrl];
    
    if (self.tabbarBtnArray.count == index) {
        [self buttonClick:tabBarBtn];
        [tabBarBtn.iconBtn setSelected:YES];
    }
    

}

- (void)ybDealSelectButton:(MyTabBarButton *)ybButton  {
    for (int i = 0; i < self.tabbarBtnArray.count; i++) {
        MyTabBarButton *currentButton = self.tabbarBtnArray[i];
        if (currentButton == ybButton) {
            [currentButton.iconBtn setSelected:YES];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.selImageArrM[i]] forState:UIControlStateSelected];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.selImageArrM[i]] forState:UIControlStateNormal];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.selImageArrM[i]] forState:UIControlStateHighlighted];
            
            currentButton.iconBtn.titleLabel.font = [UIFont fontWithName:kFuturaBold size:12];
            
        } else {
            [currentButton.iconBtn setSelected:NO];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.norImageArrM[i]] forState:UIControlStateSelected];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.norImageArrM[i]] forState:UIControlStateNormal];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.norImageArrM[i]] forState:UIControlStateHighlighted];
            
            currentButton.iconBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:12];
        }
    }
}

#pragma mark -  <MyTabBarButtonDelegate>
- (void)myTabBarIconButtonClick:(MyTabBarButton *)ybButton {
    [self buttonClick:ybButton];
}

- (void)buttonClick:(MyTabBarButton *)button {
    
    if (button.tag == 1 || button.tag == 2) {
        
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
            [[CreditCore globleCore] initLocationJudge];
            return;
        }
        [[CreditCore globleCore] initLocationJudge];
        
        if ([[[NSUserDefaults standardUserDefaults] valueForKey:kIsNeedLocation] integerValue] == 1) {
            if (![StringUtil determineWhetherTheAPPOpensTheLocation]) {
                return;
            }
        }
    }
    
    
    
    if (_clickBtnBlock) {
        _clickBtnBlock(button.tag);
    }
    
    if (![[CurrentUser mine] hasLogged] && (button.tag == 1 || button.tag == 2)) {
        return;
    }
    
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    [self ybDealSelectButton:self.selectedButton];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat height = DimensionBaseIphone6(54) + kDefaultTabBarFixHeight;
    CGRect newFrame = self.frame;
    newFrame.size.height = height;
    self.frame = newFrame;

    CGFloat buttonH = DimensionBaseIphone6(36);
    CGFloat buttonW = (self.frame.size.width - 30 -  30)/ self.tabbarBtnArray.count;
//    CGFloat buttonY = IS_IPhoneX_All ? DimensionBaseIphone6(6) : DimensionBaseIphone6(7);
    CGFloat buttonY = DimensionBaseIphone6(54 - 36)/2;
    for (int index = 0; index<self.tabbarBtnArray.count; index++) {

        MyTabBarButton *button = self.tabbarBtnArray[index];

        CGFloat buttonX = index * buttonW + 15 + 10 + 5*index;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);

        button.tag = index;
    }
    
    
}

@end
